<?php

interface Person {
    public function getName();
}

class Teacher implements Person
{
    public function getName()
    {
        return "I\m a teacher \n";
    }
}

class Student implements Person
{
    public function getName()
    {
        return "Im a student \n";
    }
}


//简单工厂实现，通过一个方法来实现相关的所有接口
class SimpleFactory
{
    public static function getPerson($type)
    {
        $person = null;
        if ($type == 'teacher') {
            $person = new Teacher();
        } elseif ($type == 'student') {
            $person = new Student();
        }
        return $person;
    }
}

//简单工厂调用
class CallSimpleFactory {
    function main() {
        $person = SimpleFactory::getPerson('teacher');
        echo $person->getName();

        $person = SimpleFactory::getPerson('student');
        echo $person->getName();
    }
}

//工厂方法
interface CommFactory
{
    public function getPerson();
}

//具体工厂实现
class StudentFactory implements CommFactory {
    function getPerson(){
        return new Student();
    }
}

class TeacherFactory implements CommFactory {
    function getPerson() {
        return new Teacher();
    }
}

//工厂方法调用
class CallCommFactory {
    static function main() {
        $factory = new TeacherFactory();
        echo $factory->getPerson()->getName();

        $factory = new StudentFactory();
        echo $factory->getPerson()->getName();
    }
}





